package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.StageWorkDao;
import com.xbongbong.saas.domain.entity.StageWorkEntity;
import com.xbongbong.saas.model.StageWorkModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/13 15:12
 * @since v1.0
 */
@Service
public class StageWorkModelImpl implements StageWorkModel {

    @Resource
    private StageWorkDao stageWorkDao;

    @Override
    public Integer insert(StageWorkEntity entity) throws XbbException {
        return stageWorkDao.insert(entity);
    }

    @Override
    public Integer update(StageWorkEntity entity) throws XbbException {
        return stageWorkDao.update(entity);
    }

    @Override
    public List<StageWorkEntity> findEntitys(Map<String, Object> param) {
        return stageWorkDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<StageWorkEntity> getDefaultList(Integer businessType) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid","0");
        map.put("businessType",businessType);
        map.put("del",0);
        return findEntitys(map);
    }

    @Override
    public void insertBatch(List<StageWorkEntity> workList) {
        stageWorkDao.insertBatch(workList);
    }

    @Override
    public void deleteBatch(List<Long> dbWorkIdList) {
        stageWorkDao.deleteBatch(dbWorkIdList);
    }

    @Override
    public void updateBatch(List<StageWorkEntity> stageWorkEntityList, String corpid) {
        stageWorkDao.updateBatch(stageWorkEntityList, corpid);
    }

    @Override
    public void updateBatch4MirrorRollback(List<StageWorkEntity> stageWorkEntityList, String corpid) {
        stageWorkDao.updateBatch4MirrorRollback(stageWorkEntityList, corpid);
    }


    @Override
    public void updateBatch4Script(List<StageWorkEntity> stageWorkEntityList) {
        stageWorkDao.updateBatch4Script(stageWorkEntityList);
    }

    @Override
    public StageWorkEntity getByKey(Long key, String corpid) {
        return stageWorkDao.getByKey(key,corpid);
    }

    @Override
    public Integer delete(StageWorkEntity entity) {
        return stageWorkDao.deleteByKey(entity.getId(), entity.getCorpid());
    }

    @Override
    public boolean isNoWorkOrProportionEqualsZero(String corpid, Long formId, Long stageProcessId, Long stageId, Integer businessType, Integer saasMark) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", 0);
        param.put("formId", formId);
        param.put("stageProcessId", stageProcessId);
        param.put("stageId", stageId);
        param.put("businessType", businessType);
        param.put("saasMark", saasMark);
        List<StageWorkEntity> stageWorkList = findEntitys(param);
        if (CollectionUtils.isEmpty(stageWorkList)) {
            return true;
        }
        int totalProportion = stageWorkList.stream().mapToInt(StageWorkEntity::getProportion).sum();
        return totalProportion == 0;
    }

    @Override
    public void sort(String corpid, Long formId, Map<Long, Integer> sortMap) {
        stageWorkDao.sort(corpid,formId,sortMap);
    }
}
